package com.zhihui.client.alarm.service.impl;

import com.zhihui.client.alarm.common.Lay;
import com.zhihui.client.alarm.common.Msg;
import com.zhihui.client.alarm.dao.UserMapper;
import com.zhihui.client.alarm.entity.User;
import com.zhihui.client.alarm.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务实现
 * @author zhihuian
 * @email 18037017565@163.com
 * @date 2018/10/29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Msg addUser(User user) {
        user.setPassword(DigestUtils.md2Hex(user.getPassword()));
        user.setUid((int) System.currentTimeMillis());

        int i=  userMapper.insert(user);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("添加成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("添加失败");
        }
        return msg;
    }

    @Override
    public Msg updateUser(User user) {
        int i= userMapper.updateByParam(user);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("修改成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @Override
    public Msg delUser(User user) {
        int i=userMapper.del(user.getUid());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("删除成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @Override
    public Lay getUsers(User user, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<User> users=userMapper.list(user);
        PageInfo info=new PageInfo(users);
        Lay lay=new Lay();
        lay.setPage(page);
        lay.setCount(info.getTotal());
        lay.setData(info.getList());
        return lay;
    }

    @Override
    public User login(String user) {
       return userMapper.login(user);
    }
}
